package com.xhermit.learn.footprint.spring.resource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

/**
 * Spring Resource 实现类测试
 * @version 3.0
 * @author FangJun
 * @date 2017年2月24日
 */
public class ResourceTest {
	
	
	@Test
	public void testByteArrayResource(){
		 Resource resource = new ByteArrayResource("Hi, June!".getBytes());
		 if(resource.exists()){
			 dumpResource(resource);
		 }
		 
	}
	
	private  void dumpResource(Resource resource){
		InputStream is=null;
		try {
			//1 获取资源
			is=resource.getInputStream();
            //2 读取资源
			byte[]  contentBytes=new byte[is.available()];
            is.read(contentBytes);
            System.out.println(new String(contentBytes));
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
				//3 关闭资源
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Test
	public void testInputResourceStream(){
		 ByteArrayInputStream bis=new ByteArrayInputStream("Hi, June!".getBytes());
		 // InputResource
	}
	
}
